// Fag.cpp : Implementation of CFag
#include "stdafx.h"
#include "EBASdll.h"
#include "Fag.h"
#include "importado.h"
#include "EnumBogpakke.h"

/////////////////////////////////////////////////////////////////////////////
// CFag

STDMETHODIMP CFag::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IFag
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP CFag::get_GUID(BSTR *pVal)
{
	*pVal = m_guid.copy();

	return S_OK;
}

STDMETHODIMP CFag::get_Navn(BSTR *pVal)
{
	*pVal = m_navn.copy();

	return S_OK;
}

STDMETHODIMP CFag::put_Navn(BSTR newVal)
{
	m_navn = newVal;

	return S_OK;
}

STDMETHODIMP CFag::get_BogPakke(IBogpakke **pVal)
{
/*
	IBogpakke * pBogpakke = NULL;
	CComObject<CEnumBogpakke> * pEnumBogpakke = NULL;
	CComObject<CEnumBogpakke>::CreateInstance(&pEnumBogpakke);
	pEnumBogpakke->findBogpakkeGUID(m_bogpakkeGuid, &pBogpakke);
	delete pEnumBogpakke;

	if (pBogpakke) {
		*pVal = pBogpakke;
		(*pVal)->AddRef();
	} else
		*pVal = NULL;
*/
	if (m_pBogpakke) {
		*pVal = m_pBogpakke;
		(*pVal)->AddRef();
	} else
		*pVal = NULL;

	return S_OK;
}

STDMETHODIMP CFag::put_BogPakke(IBogpakke *newVal)
{
	m_pBogpakke = newVal;
	if (m_pBogpakke)
		m_pBogpakke->AddRef();

	return S_OK;
}

STDMETHODIMP CFag::get_KarakterSkala(IKarakterSkala **pVal)
{
	if (m_pKarakterSkala) {
		*pVal = m_pKarakterSkala;
		(*pVal)->AddRef();
	} else
		*pVal = NULL;

	return S_OK;
}

STDMETHODIMP CFag::put_KarakterSkala(IKarakterSkala *newVal)
{
	m_pKarakterSkala = newVal;
	if (m_pKarakterSkala)
		m_pKarakterSkala->AddRef();

	return S_OK;
}

STDMETHODIMP CFag::SetInfo()
{
	_CommandPtr command;
	_RecordsetPtr recordset;

	const char sql[255] = "EBAS_SP_GEM_FAG";

	try {
		BSTR	pSkala = bstr_t(""), 
				pBogpakke = bstr_t("");
		bstr_t	skala = bstr_t(""), 
				bogpakke = bstr_t("");
		
		if (NULL != m_pBogpakke) {	
			m_pBogpakke->get_Navn(&pBogpakke);
			bogpakke = pBogpakke;
		}
		
		if (NULL == m_pKarakterSkala)
			return E_INVALIDARG;

		m_pKarakterSkala->get_Navn(&pSkala);
		skala = pSkala;

		command.CreateInstance(__uuidof(Command));		
		command->ActiveConnection = CDatabase::GetConnection();
		command->CommandText = sql;
		command->CommandType = adCmdStoredProc;
		command->Parameters->Append( command->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		command->Parameters->Append( command->CreateParameter("navn", adVarChar, adParamInput, 255, m_navn) );
		command->Parameters->Append( command->CreateParameter("karakterSkala", adVarChar, adParamInput, 255, skala) );
		if (bstr_t("") != bogpakke)
			command->Parameters->Append( command->CreateParameter("bogpakke", adVarChar, adParamInput, 255, bstr_t(bogpakke)) );
		
		recordset = command->Execute(NULL, NULL, adCmdStoredProc);
		if(recordset->ADO_EOF)
			return E_ACCESSDENIED;

		m_guid = (bstr_t)recordset->Fields->GetItem("GUID")->Value;
		
		if (recordset)
			recordset->Close();
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}
	return S_OK;
}

STDMETHODIMP CFag::Slet()
{
	_CommandPtr command;
	_RecordsetPtr recordset;

	const char sql[255] = "EBAS_SP_SLET_FAG";

	try {
		command.CreateInstance(__uuidof(Command));		
		command->ActiveConnection = CDatabase::GetConnection();
		command->CommandText = sql;
		command->CommandType = adCmdStoredProc;
		command->Parameters->Append( command->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		command->Parameters->Append( command->CreateParameter("fagGuid", adVarChar, adParamInput, 255, m_guid) );
		
		recordset = command->Execute(NULL, NULL, adCmdStoredProc);
		if(recordset->ADO_EOF)
			return E_ACCESSDENIED;

		m_guid = bstr_t("");
		m_navn = bstr_t("");
		m_bogpakkeGuid = bstr_t("");
		m_pBogpakke = NULL;
		m_pKarakterSkala = NULL;

		if (recordset)
			recordset->Close();
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}
	return S_OK;
}
